package com.neusoft.nep.NEPM.controller;

import com.neusoft.nep.entity.AqiData;
import com.neusoft.nep.entity.MonthData;
import com.neusoft.nep.entity.ProvinceData;
import com.neusoft.nep.entity.nepgResult;
import com.neusoft.tools.AlertUtils;
import com.neusoft.tools.FXMLUtils;
import com.neusoft.tools.FileUtils;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.*;

import com.neusoft.tools.FeedbackUtils;

import static com.neusoft.tools.FeedbackUtils.getAllFeedback;

public class AqiSeperatedStatisViewController {
    @FXML
    public Label adminLabel;
    @FXML
    TreeItem to_detailOfAqiView;
    @FXML
    TreeView treeview;
    @FXML
    private TableView tableview;
    @FXML
    private TableColumn col_grade;
    @FXML
    private TableColumn col_dec;
    @FXML
    private TableColumn col_num;
    @FXML
    TreeItem to_publicAqiDataListView;
    @FXML
    private Button btn_out;
    @FXML
    private TreeItem To_AqiTrendTotalStatisView;
    @FXML
    private TreeItem To_AqiSeperatedStatisView;
    @FXML
    private TreeItem To_OtherStatisView;
    @FXML
    private TreeItem To_ProvinceDataTotalStatisView;
    @FXML
    TreeItem To_confirmDataView;
    @FXML
    TreeItem To_publicAqiDataList;
    @FXML
    private AnchorPane root;
    @FXML
    private Line line1;
    @FXML
    private Line line2;
    private ObservableList LastData= FXCollections.observableArrayList();
    private Map Aqimap;
    @FXML
    public void initialize() {
        treeview.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue == To_AqiSeperatedStatisView) {
                try {
                    FXMLUtils.switchToShow(btn_out, "NEPM/view/AqiSeperatedStatisView.fxml");
                } catch (IOException e) {
                    e.printStackTrace();
                    AlertUtils.showDialog("错误", "页面跳转失败: " + e.getMessage());
                }
            } else if (newValue == To_AqiTrendTotalStatisView) {
                try {
                    FXMLUtils.switchToShow(btn_out, "NEPM/view/AqiTrendTotalStatisView.fxml");
                } catch (IOException e) {
                    e.printStackTrace();
                    AlertUtils.showDialog("错误", "页面跳转失败: " + e.getMessage());
                }
            } else if (newValue == To_OtherStatisView) {
                try {
                    FXMLUtils.switchToShow(btn_out, "NEPM/view/OtherStatisView.fxml");
                } catch (IOException e) {
                    e.printStackTrace();
                    AlertUtils.showDialog("错误", "页面跳转失败: " + e.getMessage());
                }
            } else if (newValue == To_ProvinceDataTotalStatisView) {
                try {
                    FXMLUtils.switchToShow(btn_out, "NEPM/view/ProvinceDataTotalStatisView.fxml");
                } catch (IOException e) {
                    e.printStackTrace();
                    AlertUtils.showDialog("错误", "页面跳转失败: " + e.getMessage());
                }

            } else if (newValue == to_publicAqiDataListView) {
                try {
                    FXMLUtils.switchToShow(btn_out, "NEPM/view/publicAqiDataListView.fxml");
                } catch (IOException e) {
                    e.printStackTrace();
                    AlertUtils.showDialog("错误", "页面跳转失败: " + e.getMessage());
                }
            } else if (newValue == To_confirmDataView) {
                try {
                    FXMLUtils.switchToShow(btn_out, "NEPM/view/confirmDataView.fxml");
                } catch (IOException e) {
                    e.printStackTrace();
                    AlertUtils.showDialog("错误", "页面跳转失败: " + e.getMessage());
                }
            } else if (newValue == To_publicAqiDataList) {
                try {
                    FXMLUtils.switchToShow(btn_out, "NEPM/view/publicAqiDataList.fxml");
                } catch (IOException e) {
                    e.printStackTrace();
                    AlertUtils.showDialog("错误", "页面跳转失败: " + e.getMessage());
                }
            } else if (newValue == to_detailOfAqiView) {
                try {
                    FXMLUtils.switchToShow(btn_out, "NEPM/view/detailOfAqiView.fxml");
                } catch (IOException e) {
                    e.printStackTrace();
                    AlertUtils.showDialog("错误", "页面跳转失败: " + e.getMessage());
                }
            }
        });
        col_grade.setCellValueFactory(new PropertyValueFactory<>("level"));
        col_num.setCellValueFactory(new PropertyValueFactory<>("Aqi_num"));
        col_dec.setCellValueFactory(new PropertyValueFactory<>("Description"));
        col_grade.prefWidthProperty().bind(tableview.widthProperty().multiply(0.25));
        col_dec.prefWidthProperty().bind(tableview.widthProperty().multiply(0.5));
        col_num.prefWidthProperty().bind(tableview.widthProperty().multiply(0.25));
        List<nepgResult> RecordData=new ArrayList<>();
        RecordData=FileUtils.readListObject("NEPGresult.txt");
        query_aqiData();
        for(nepgResult item:RecordData){
            AqiData aqiData;
            String realLevel= item.getRealLevel();
            if (realLevel == null || !Aqimap.containsKey(realLevel)) {
                System.out.println("无效的AQI等级: " + realLevel);
                continue; // 跳过无效记录
            }
            aqiData=(AqiData) Aqimap.get(item.getRealLevel());
            int nowAqinum=aqiData.getAqi_num();
            nowAqinum++;
            aqiData.setAqi_num(nowAqinum);
        }
        LastData.addAll(Aqimap.values());
        tableview.setItems(LastData);
        tableview.prefWidthProperty().bind(root.widthProperty());
        tableview.prefHeightProperty().bind(root.heightProperty());
        line1.endXProperty().bind(root.widthProperty());
        line2.endXProperty().bind(root.widthProperty());
    }

    public void Log_out() throws IOException {
        FXMLUtils.switchToShow(btn_out, "NEPM/view/LogInView.fxml");
    }

    public void back() throws IOException {
        FXMLUtils.switchToShow(btn_out, "NEPM/view/ManagerUIView.fxml");
    }
    public void query_aqiData(){
        Aqimap = new LinkedHashMap<String, AqiData>();
        Aqimap.put("一",new AqiData("一","优",0));
        Aqimap.put("二",new AqiData("二","良",0));
        Aqimap.put("三",new AqiData("三","轻度污染",0));
        Aqimap.put("四",new AqiData("四","中度污染",0));
        Aqimap.put("五",new AqiData("五","重度污染",0));
        Aqimap.put("六",new AqiData("六","严重污染",0));
    }
}
